package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.OfficeSatisfactionMain;
import com.example.ymm.modules1.mapper.my.user.OfficeSatisfactionMainMapper;
import com.example.ymm.modules1.query.OfficeSatisfactionMainQueryCriteria;
import com.example.ymm.modules1.service.user.OfficeSatisfactionMainService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class OfficeSatisfactionMainServiceImpl extends BaseServiceImpl<OfficeSatisfactionMainMapper, OfficeSatisfactionMain> implements OfficeSatisfactionMainService {

    @Override
    public TableList<OfficeSatisfactionMain> list(OfficeSatisfactionMainQueryCriteria criteria, YmPageDto pageDto) {
        IPage<OfficeSatisfactionMain> page = new Page<OfficeSatisfactionMain>(pageDto.getPage(),pageDto.getPageSize());
        QueryWrapper predicate = QueryHelpPlus.getPredicate(OfficeSatisfactionMain.class, criteria);
        predicate.orderByDesc("create_time");
        baseMapper.selectPage(page, predicate);
        TableList<OfficeSatisfactionMain> tableList=new TableList(page);
        return tableList;
    }

    public List<OfficeSatisfactionMain> queryAll(OfficeSatisfactionMainQueryCriteria criteria) {
        List<OfficeSatisfactionMain> result =baseMapper.selectList(QueryHelpPlus.getPredicate(OfficeSatisfactionMain.class, criteria));
        return result;
    }

    @Override
    public OfficeSatisfactionMain getByUserIdAndYear(String userId, String year) {
        if(UtilValidate.isEmpty(year)){
            return null;
        }
        OfficeSatisfactionMainQueryCriteria criteria=new OfficeSatisfactionMainQueryCriteria();
        criteria.setUserId(userId);
        criteria.setYear(year);
        List<OfficeSatisfactionMain> officeSatisfactionMains = queryAll(criteria);
        if(officeSatisfactionMains.size()>0){
            return officeSatisfactionMains.get(0);
        }
        return null;
    }
}
